﻿using UnityEngine;
using System.Collections;

/// <summary>
/// 骷髅怪动画行为脚本
/// </summary>
public class SkeletonAnimationBehaviour : StateMachineBehaviour {

    /// <summary>
    /// 骷髅行为脚本对象
    /// </summary>
    public SkeletonBehaviour skeleton_behaviour;
    
    /// <summary>
    /// 如果为true，则播放死亡动画，并阻止任何动画播放
    /// </summary>
    public bool death;
    /// <summary>
    /// 如果为true，则播放被攻击动画
    /// </summary>
    public bool damage;
    /// <summary>
    /// 如果为true，则播放攻击动画
    /// </summary>
    public bool attack;
    /// <summary>
    /// 如果为true，则播放移动动画
    /// </summary>
    public bool start_locomotion;
    /// <summary>
    /// 如果为true，则停止移动动画
    /// </summary>
    public bool end_locomotion;
    /// <summary>
    /// 如果为true，则播放警示动画（注视玩家）
    /// </summary>
    public bool warn;
    /// <summary>
    /// 为0时移动为慢走动画，为1时移动为奔跑动画
    /// </summary>
    public float speed;

    /// <summary>
    /// 骷髅怪的角色控制器
    /// </summary>
    CharacterController controller { get { return skeleton_behaviour.Controller; } }

    #region 预存的参数哈希值，提高效率
    int f_speed = Animator.StringToHash("speed");
    int t_damage = Animator.StringToHash("damage");
    int t_death = Animator.StringToHash("death");
    int t_attack = Animator.StringToHash("attack");
    int b_locomotion = Animator.StringToHash("locomotion");
    int b_warn = Animator.StringToHash("warn");
    #endregion

    #region 预存的动画名哈希值，提高效率
    int s_death = Animator.StringToHash("Death");
    int s_locomotion = Animator.StringToHash("Locomotion");
    int s_attack = Animator.StringToHash("Attack");
    int s_warn = Animator.StringToHash("Warn"); 
    #endregion

    // OnStateEnter is called before OnStateEnter is called on any state inside this state machine
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if(stateInfo.shortNameHash == s_attack)
        {
            //若开始播放攻击动画，通知行为脚本进行攻击判断
            skeleton_behaviour.Attack();

            
        }
    }

    // OnStateUpdate is called before OnStateUpdate is called on any state inside this state machine
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        //如果当前状态为死亡状态，则直接返回
        if(stateInfo.shortNameHash == s_death)
            return;

        if(warn)
            animator.SetBool(b_warn, true);
        else if(stateInfo.shortNameHash == s_warn)
            animator.SetBool(b_warn, false);

        if (start_locomotion)
        {
            animator.SetBool(b_locomotion, true);
        }
        if (end_locomotion)
        {
            animator.SetBool(b_locomotion, false);
        }
        start_locomotion = end_locomotion = false;
        animator.SetFloat(f_speed, speed);

        if(attack)
        {
            animator.SetBool(b_locomotion, false);
            animator.SetTrigger(t_attack);
            attack = !attack;
        }
        if (damage && !death)
        {
            damage = false;
            animator.SetTrigger(t_damage);
        }
        if (death)
        {
            animator.SetTrigger(t_death);
            return;
        }

        var transform = skeleton_behaviour.transform;
        if (stateInfo.shortNameHash == s_locomotion)
        {
            //如果当前动画为移动动画，则通过控制器控制进行移动
            controller.SimpleMove(transform.forward.normalized * speed);
        }
        else if(stateInfo.shortNameHash == s_attack)
        {
            skeleton_behaviour.transform.LookAt(GameSettings.Instance.Player.transform);
        }
    }


    //OnStateExit is called before OnStateExit is called on any state inside this state machine
    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
    }

    // OnStateMove is called before OnStateMove is called on any state inside this state machine
    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //}

    // OnStateIK is called before OnStateIK is called on any state inside this state machine
    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}

    // OnStateMachineEnter is called when entering a statemachine via its Entry Node
    //override public void OnStateMachineEnter(Animator animator, int stateMachinePathHash)
    //{
    //}

    // OnStateMachineExit is called when exiting a statemachine via its Exit Node
    //override public void OnStateMachineExit(Animator animator, int stateMachinePathHash)
    //{
    //}
}
